home *** CD-ROM | disk | FTP | other *** search
/ X User Tools / X User Tools (O'Reilly and Associates)(1994).ISO / sources / libxpm / libxpm34.gz / libxpm34 / xpm-3.4 / lib / rgb.c < prev    next >
C/C++ Source or Header  |  1994-03-14  |  4KB  |  137 lines

  1. /* Copyright 1989-94 GROUPE BULL -- See license conditions in file COPYRIGHT */
  2. /*****************************************************************************\
  3. * rgb.c:                                                                      *
  4. *                                                                             *
  5. *  XPM library                                                                *
  6. *  Rgb file utilities                                                         *
  7. *                                                                             *
  8. *  Developed by Arnaud Le Hors                                                *
  9. \*****************************************************************************/
  10.  
  11. /*
  12.  * Part of this code has been taken from the ppmtoxpm.c file written by Mark
  13.  * W. Snitily but has been modified for my special need
  14.  */
  15.  
  16. #include "xpmP.h"
  17. #ifdef VMS
  18. #include "sys$library:ctype.h"
  19. #include "sys$library:string.h"
  20. #else
  21. #include <ctype.h>
  22. #if defined(SYSV) || defined(SVR4)
  23. #include <string.h>
  24. #else
  25. #include <strings.h>
  26. #endif
  27. #endif
  28.  
  29. /*
  30.  * Read a rgb text file.  It stores the rgb values (0->65535)
  31.  * and the rgb mnemonics (malloc'ed) into the "rgbn" array.  Returns the
  32.  * number of entries stored.
  33.  */
  34. int
  35. xpmReadRgbNames(rgb_fname, rgbn)
  36.     char *rgb_fname;
  37.     xpmRgbName rgbn[];
  38.  
  39. {
  40.     FILE *rgbf;
  41.     int i, items, red, green, blue;
  42.     char line[512], name[512], *rgbname, *n, *m;
  43.     xpmRgbName *rgb;
  44.  
  45.     /* Open the rgb text file.  Abort if error. */
  46.     if ((rgbf = fopen(rgb_fname, "r")) == NULL)
  47.     return 0;
  48.  
  49.     /* Loop reading each line in the file. */
  50.     for (i = 0, rgb = rgbn; fgets(line, sizeof(line), rgbf); i++, rgb++) {
  51.  
  52.     /* Quit if rgb text file is too large. */
  53.     if (i == MAX_RGBNAMES) {
  54.         /* Too many entries in rgb text file, give up here */
  55.         break;
  56.     }
  57.     /* Read the line.  Skip silently if bad. */
  58.     items = sscanf(line, "%d %d %d %[^\n]\n", &red, &green, &blue, name);
  59.     if (items != 4) {
  60.         i--;
  61.         continue;
  62.     }
  63.  
  64.     /*
  65.      * Make sure rgb values are within 0->255 range. Skip silently if
  66.      * bad.
  67.      */
  68.     if (red < 0 || red > 0xFF ||
  69.         green < 0 || green > 0xFF ||
  70.         blue < 0 || blue > 0xFF) {
  71.         i--;
  72.         continue;
  73.     }
  74.     /* Allocate memory for ascii name. If error give up here. */
  75.     if (!(rgbname = (char *) XpmMalloc(strlen(name) + 1)))
  76.         break;
  77.  
  78.     /* Copy string to ascii name and lowercase it. */
  79.     for (n = name, m = rgbname; *n; n++)
  80.         *m++ = isupper(*n) ? tolower(*n) : *n;
  81.     *m = '\0';
  82.  
  83.     /* Save the rgb values and ascii name in the array. */
  84.     rgb->r = red * 257;        /* 65535/255 = 257 */
  85.     rgb->g = green * 257;
  86.     rgb->b = blue * 257;
  87.     rgb->name = rgbname;
  88.     }
  89.  
  90.     fclose(rgbf);
  91.  
  92.     /* Return the number of read rgb names. */
  93.     return i < 0 ? 0 : i;
  94. }
  95.  
  96. /*
  97.  * Return the color name corresponding to the given rgb values
  98.  */
  99. char *
  100. xpmGetRgbName(rgbn, rgbn_max, red, green, blue)
  101.     xpmRgbName rgbn[];            /* rgb mnemonics from rgb text file */
  102.     int rgbn_max;            /* number of rgb mnemonics in table */
  103.     int red, green, blue;        /* rgb values */
  104.  
  105. {
  106.     int i;
  107.     xpmRgbName *rgb;
  108.  
  109.     /*
  110.      * Just perform a dumb linear search over the rgb values of the color
  111.      * mnemonics.  One could speed things up by sorting the rgb values and
  112.      * using a binary search, or building a hash table, etc...
  113.      */
  114.     for (i = 0, rgb = rgbn; i < rgbn_max; i++, rgb++)
  115.     if (red == rgb->r && green == rgb->g && blue == rgb->b)
  116.         return rgb->name;
  117.  
  118.     /* if not found return NULL */
  119.     return NULL;
  120. }
  121.  
  122. /*
  123.  * Free the strings which have been malloc'ed in xpmReadRgbNames
  124.  */
  125. void
  126. xpmFreeRgbNames(rgbn, rgbn_max)
  127.     xpmRgbName rgbn[];
  128.     int rgbn_max;
  129.  
  130. {
  131.     int i;
  132.     xpmRgbName *rgb;
  133.  
  134.     for (i = 0, rgb = rgbn; i < rgbn_max; i++, rgb++)
  135.     XpmFree(rgb->name);
  136. }
  137.